package com.meirengu.channel.service.impl;

import com.meirengu.channel.service.ZhuDaiService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwZhudaiPush;
import com.meirengu.webview.model.KwZhudaiPushLog;
import com.meirengu.webview.service.KwZhudaiPushLogService;
import com.meirengu.webview.service.KwZhudaiPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 平安投放
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class ZhuDaiServiceImpl implements ZhuDaiService {

    Logger logger = LoggerFactory.getLogger(ZhuDaiServiceImpl.class);
    @Autowired
    private KwZhudaiPushService kwZhudaiPushService;

    @Autowired
    private KwZhudaiPushLogService kwZhudaiPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String name, String mobile, String city, String car,
                             int age, String job, String house, String baodanIs,
                             String sex, int money, String shebao, String gongjijin,
                             String isbankpay, String ip, String creditCard,
                             String meiti, String time) throws Exception {

        String url = ConfigUtil.getConfig("zhudai.url");
        String source = ConfigUtil.getConfig("zhudai.source");

        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("mobile", mobile);
        params.put("city", city);
        params.put("car", car);
        params.put("age", age + "");
        params.put("job", job);
        params.put("house", house);
        params.put("baodan_is", baodanIs);
        params.put("sex", sex);
        params.put("money", money + "");
        params.put("source", source);
        params.put("shebao", shebao);
        params.put("gongjijin", gongjijin);
        params.put("isbankpay", isbankpay);
        params.put("ip", ip);
        params.put("credit_card", creditCard);
        params.put("meiti", meiti);
        /*时间改为实时，通过控制单位时间内的传送数量来达到自然用户注册的假象*/
        params.put("time", time.substring(0, 10));
        /*long registerTime = System.currentTimeMillis()-5000;
        logger.info("用户注册时间：{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(registerTime)));
        String timeStr = registerTime + "";
        params.put("time", timeStr.substring(0,10));
*/

        //接口调用信息表
        KwZhudaiPush kwZhudaiPush = new KwZhudaiPush();
        kwZhudaiPush.setInterfaceCode("zhudai");
        kwZhudaiPush.setInterfaceName("助贷网");
        kwZhudaiPush.setMobile(mobile);
        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwZhudaiPushLog kwZhudaiPushLog = new KwZhudaiPushLog();
        kwZhudaiPushLog.setInterfaceCode("zhudai");
        kwZhudaiPushLog.setInterfaceName("助贷网");
        kwZhudaiPushLog.setMobile(mobile);
        kwZhudaiPushLog.setSendUrl(url.toString());
        kwZhudaiPushLog.setParams(params.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwZhudaiPushLog.setBackUrl(hr.toString());
                kwZhudaiPushLog.setHttpCode(200);
                kwZhudaiPushLog.setHttpContent(hr.toString());

                logger.info("==ZhuDaiServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                Integer rCode = 0;
                rCode = Integer.parseInt(content);
                if (rCode.intValue() > 1000000) {
                    kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwZhudaiPush.setMessage("成功");
                    logger.info("==ZhuDaiServiceImpl.applyLoan business success");
                } else if (rCode.intValue() == 3) {
                    kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwZhudaiPush.setMessage("rCode: " + rCode);
                    logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                } else {
                    kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwZhudaiPush.setMessage("rCode: " + rCode);
                    logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                }
            } else {
                kwZhudaiPushLog.setBackUrl(hr.toString());
                kwZhudaiPushLog.setHttpCode(code);
                kwZhudaiPushLog.setHttpContent(hr.toString());
                logger.warn("==ZhuDaiServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwZhudaiPushLog.setHttpCode(408);
            kwZhudaiPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwZhudaiPushService.insert(kwZhudaiPush);
            kwZhudaiPushLog.setPushId(kwZhudaiPush.getId());
            kwZhudaiPushLogService.insert(kwZhudaiPushLog);
            return kwZhudaiPush.getCode();
        }
    }

}
